<?php
/**
 * 
 * @author Radovan Dvorsky
 * @version 20100410
 *
 */
abstract class My_Form_AjaxForm extends Zend_Form{
	
	private $_jqueryParams = array();	
	private $_ajaxableElements = array();	
	
	public function __construct($options = null) {
		parent::__construct($options);
	}
	
	
	public function addAjaxableElement(Zend_Form_Element $element){		
		
		if(!($element instanceof Zend_Form_Element)){
			throw new My_Form_Exception('$element is not instance of Zend_Form_Element');
		}	
		
		$this->_ajaxableElements[$element->getName()] = $element;		
	}
	
	
	public function addAjaxableElements(array $elements){
		
		if(!is_array($elements)){
			throw new My_Form_Exception('$elements must be an array');
		}
				
		foreach($elements as $element){			
			$this->addAjaxableElement($element);
		}
	}
	
	
	/**
	 * @return the $_ajaxableElements
	 */
	public function getAjaxableElements() {
		return $this->_ajaxableElements;
	}	
	
	
	/**
	 * url - adresa kam sa odosle ajax pozadavok
	 * update - element selector do ktoreho sa vlozi vystup
	 * format - html,json...
	 * 
	 * @param  array $_jqueryParams
	 */
	public function setJqueryParams(array $jqueryParams) {
		
		if(!is_array($jqueryParams)){
			throw new My_Form_Exception('$jqueryParams must be an array');
		}
		
		$this->_jqueryParams = $jqueryParams;
	}

	/**
	 * @return the $_jqueryParams
	 */
	public function getJqueryParams() {
		return $this->_jqueryParams;
	}
	
	
	public function setSender(Zend_Form_Element $element, $event = 'onClick'){
		
		if(!$element instanceof Zend_Form_Element){
			throw new My_Form_Exception('$element is not instance of Zend_Form_Element');
		}
		
		if(empty($this->_ajaxableElements)){
			throw new My_Form_Exception('$ajaxableElements is not set');
		}
		
		$element->setAttrib($event,$this->createJqueryPost());				
	}
	
	public function addParam($key,$value){
		$this->_jqueryParams['params'][$key] = $value;
	}
	
	public function addParams(array $params){
		if(!is_array($params)){
			throw new My_Form_Exception('$params is not an array');
		}
		
		foreach($params as $key => $value){
			$this->addParam($key,$value);
		}
	}
	
	private function createJqueryPost(){
				
		$variables = '{';
		$addVariables = '';
		
		foreach($this->_ajaxableElements as $ajaxElement){			
			
			switch ($ajaxElement->getType()){
				case 'Zend_Form_Element_Text': $type = 'input';
					break;
				case 'Zend_Form_Element_Textarea': $type = 'textarea';
					break;
				case 'Zend_Form_Element_Select': $type = 'select';
					break;
				default: $type = 'input';
					break;
			}
						
			$variables .= $ajaxElement->getName() . ':' . '$("form ' . $type . '[name=' . $ajaxElement->getName() . ']").val(),';
		}
		
		if(isset($this->_jqueryParams['params'])){
			foreach($this->_jqueryParams['params'] as $key => $value){
				$addVariables .= $key . ':"' . $value . '",';
			}		
		}
		$variables .= $addVariables;
		$variables = rtrim($variables,',');
		$variables .= '}';		
				
		isset($this->_jqueryParams['append']) ? $function = 'append(data)' : $function = 'html(data)';		
		isset($this->_jqueryParams['destroyDialog']) ? $destroy = '$("#dialogContainer").dialog("close");' : $destroy = null;
		return '$.post("' . $this->_jqueryParams['url'] . '",' . $variables . ',
				function(data){
					$("' . $this->_jqueryParams['update'] . '").' . $function . ';
					' . $destroy . '
				},"' . $this->_jqueryParams['format'] . '")';die();		
	}	
}